package cn.iocoder.yudao.module.erp.service.business.produce.produceprocess;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.produceprocess.vo.ProduceProcessPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.produceprocess.vo.ProduceProcessSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.produce.produceprocess.ProduceProcessDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.produce.produceprocess.ProduceProcessMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PRODUCE_PROCESS_NOT_EXISTS;

/**
 * 工序 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class ProduceProcessServiceImpl implements ProduceProcessService {

    @Resource
    private ProduceProcessMapper produceProcessMapper;

    @Override
    public Long createProduceProcess(ProduceProcessSaveReqVO createReqVO) {
        // 插入
        ProduceProcessDO produceProcess = BeanUtils.toBean(createReqVO, ProduceProcessDO.class);
        produceProcessMapper.insert(produceProcess);
        // 返回
        return produceProcess.getId();
    }

    @Override
    public void updateProduceProcess(ProduceProcessSaveReqVO updateReqVO) {
        // 校验存在
        validateProduceProcessExists(updateReqVO.getId());
        // 更新
        ProduceProcessDO updateObj = BeanUtils.toBean(updateReqVO, ProduceProcessDO.class);
        produceProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteProduceProcess(Long id) {
        // 校验存在
        validateProduceProcessExists(id);
        // 删除
        produceProcessMapper.deleteById(id);
    }

    private void validateProduceProcessExists(Long id) {
        if (produceProcessMapper.selectById(id) == null) {
            throw exception(PRODUCE_PROCESS_NOT_EXISTS);
        }
    }

    @Override
    public ProduceProcessDO getProduceProcess(Long id) {
        return produceProcessMapper.selectById(id);
    }

    @Override
    public PageResult<ProduceProcessDO> getProduceProcessPage(ProduceProcessPageReqVO pageReqVO) {
        return produceProcessMapper.selectPage(pageReqVO);
    }

}